﻿using Engine.AddinManager;
using Engine.Graphics;
using Engine.Shape;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SharpDX;
using System.Collections.Generic;

namespace UnitTestsScenarioEditor
{
    [TestClass()]
    public class SceneryObjectTest
    {
        #region Private Fields

        private static EngineInit engine;

        #endregion Private Fields

        #region Public Methods

        [ClassInitialize()]
        public static void ClassInit(TestContext context)
        {
            engine = new EngineInit();
        }

        public static SceneryObject CreateSampleSceneryObject(string addinName)
        {
            //engine = new EngineInit();
            engine.engine.addinManager.SetAddin(addinName);

            IAddin addin = engine.engine.addinManager.CurrentAddin;
            SceneryObject sceneryObject = new SceneryObject(engine.engine);
            List<IGameVertex> gameVertices = new List<IGameVertex>();
            List<Engine.Graphics.VertexStructures.MyOwnVertex.PositionNormalTextured> vertices = new List<Engine.Graphics.VertexStructures.MyOwnVertex.PositionNormalTextured>();
            List<ushort> indices = new List<ushort>() { 0, 1, 2 };
            List<Material> materials = new List<Material>();
            List<int> numberOfMaterials = new List<int>() { 1 };

            // Vertices
            vertices.Add(new Engine.Graphics.VertexStructures.MyOwnVertex.PositionNormalTextured(new SharpDX.Vector3(0, 0, 0), SharpDX.Vector3.Up, new SharpDX.Vector2(0, 0)));
            vertices.Add(new Engine.Graphics.VertexStructures.MyOwnVertex.PositionNormalTextured(new SharpDX.Vector3(100, 0, 100), SharpDX.Vector3.Up, new SharpDX.Vector2(1, 0)));
            vertices.Add(new Engine.Graphics.VertexStructures.MyOwnVertex.PositionNormalTextured(new SharpDX.Vector3(100, 0, 0), SharpDX.Vector3.Up, new SharpDX.Vector2(1, 1)));

            // Create game vertices
            foreach (var item in vertices)
            {
                gameVertices.Add(addin.CreateGameVertice(item));
            }
            // Materials
            materials.Add(new Material(addin.CreateEmptyMaterial(engine.materialManager)));

            // Crete mesh
            var mesh = new Engine.FileFormats3D.Mesh3dData(indices, vertices, gameVertices, numberOfMaterials, materials, engine.engine.addinManager, addin);

            // Shape
            Engine.Shape.Shapes shape = new Engine.Shape.Shapes(engine.engine.Grafic.Device);
            List<SharpDX.Vector3> borderPositions = new List<SharpDX.Vector3>();
            borderPositions.Add(new SharpDX.Vector3(0, 0, 0));
            borderPositions.Add(new SharpDX.Vector3(100, 0, 0));
            borderPositions.Add(new SharpDX.Vector3(100, 0, 100));
            borderPositions.Add(new SharpDX.Vector3(0, 0, 0));

            Border border = new Border(borderPositions);
            shape.AddBorder(border);

            sceneryObject.ShortFilename = "TestObject";
            sceneryObject.MeshData = mesh;
            sceneryObject.Shape = shape;
            sceneryObject.Update(Vector3.Zero, 0.0f, 1.0f);

            return sceneryObject;
        }

        [TestMethod()]
        public void TestSaveLoadSceneryObject()
        {
            string sceneryObjectFilename = @"C:\Dev\Scenario Editor\bin\UnitTests\Scenery Object.Zip";
            var sceneryObject = CreateSampleSceneryObject("World Racing 2");

            sceneryObject.SaveToZip(sceneryObjectFilename);

            SceneryObject loadedSceneryObject = SceneryObject.FromZipFile(sceneryObjectFilename, engine.engine, engine.materialManager);

            // TODO: compare values.
        }

        #endregion Public Methods
    }
}